package com.wdc.controller;

import com.wdc.entity.Author;
import com.wdc.entity.Book;

import java.security.Key;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AuthorsController {
    public static void main(String[] args) {
        List<Author> author = getAuthors();
//        author.stream()
//                .distinct()
//                .filter(author1 -> author1.getAge() <= 18)
//                .forEach(author12 -> System.out.println(author12.getName()));


//        author.stream()
//                .map(Author::getName)    //Author::getName
//                .forEach(System.out::println);    //System.out::println
//
//        author.stream()
//                .map(Author::getAge)
//                .map(age -> age +10)
//                .forEach(System.out::println);


//        author.stream()
//                .distinct()
//                .map(Author::getAge)
//                .sorted((o1, o2) -> o2 - o1)
//
//                .forEach(System.out::println);



//        author.stream()
//                .distinct()
//                .sorted((o1, o2) -> o2.getAge() - o1.getAge())
//                .limit(2)
//                .forEach(author1 -> System.out.println(author1.getName() +"的年龄为:" + author1.getAge()));


//
//        author.stream()
//                .distinct()
//                .sorted((o1, o2) -> o2.getAge() - o1.getAge())
//                .skip(1)
//                .forEach(au -> System.out.println(au.getName()));


//        author.stream()
//                .map(author1 -> author1.getBooks())
//                .forEach(System.out::println);

//        author.stream()
//                .flatMap( author1 -> author1.getBooks().stream())
//                .distinct()
//                .forEach(book -> System.out.println(book.getName()));

//        author.stream()
//                .flatMap( author1 -> author1.getBooks().stream())
//                .distinct()
//                .flatMap( book -> Arrays.stream(book.getCategory().split(",")))
//                .distinct()
//                .forEach(System.out::println);

//        Optional<Integer> max = author.stream()
//                .flatMap(author1 -> author1.getBooks().stream())
//                .map(book -> book.getScore())
//                .max((o1, o2) -> o2 - o1);
//        System.out.println(max.get());

//        Set<String> collect = author.stream()
//                .flatMap(author1 -> author1.getBooks().stream())
//                .map(book -> book.getName())
//                .collect(Collectors.toSet());
//
//        System.out.println(collect);

//        Map<String, List<Book>> map = author.stream()
//                .distinct()
//                .collect(Collectors.toMap(author1 -> author1.getName(), author12 -> author12.getBooks()));
//        System.out.println(map);

//        Optional<Integer> first = author.stream()
//                .map(Author::getAge)
//                .sorted(new Comparator<Integer>() {
//                    @Override
//                    public int compare(Integer o1, Integer o2) {
//                        return o2 - o1;
//                    }
//                })
//                .findFirst();
//        System.out.println(first.get() );

        //使用reduce 求所有作家当中年龄的最大值
//        Integer reduce = author.stream()
//                .distinct()
//                .map(author1 -> author1.getAge())
//                .reduce(Integer.MIN_VALUE, (result, element) -> result < element ? element : result);
//        System.out.println(reduce);


        Author au = getAuthor();
        Optional<Author> authorOptional = Optional.ofNullable(au);
        authorOptional.ifPresent(author1 -> System.out.println(author1.getName()));


    }

    private static Author getAuthor(){
        Author author = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null);

        return null;
    }


    private static List<Author> getAuthors(){
        //数据初始化
        Author author = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null);
        Author author2 = new Author(2L,"亚拉索" ,15,"狂风也追逐不上他的思考速度",null);
         Author author3 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);
        Author author4 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);
                
        //书籍列表
        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();

        books1.add(new Book(1L,"刀的两侧是光明与黑暗","哲学,爱情",88,"用一把刀划分了爱恨"));

        books1.add(new Book(1L,"刀的两侧是光明与黑暗","哲学,爱情",88,"用一把刀划分了爱恨"));
        books1.add(new Book(2L,"一个人不能死在同一把刀下","个人成长,爱情",99,"讲述如何从失败中明悟真理"));
        books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(4L,"吹或不吹","爱情,个人传记",56,"一个哲学家的恋爱观注定很难把他所在的时代理解"));
        books3.add(new Book(5L,"你的剑就是我的剑","爱情",56,"无法想象一个武者能对他的伴侣这么宽容"));
        books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?"));
        books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢?"));

        author.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books3);
        author4.setBooks(books3);
        List<Author> authorList = new ArrayList<>(Arrays.asList(author ,author2,author3,author4));
        return authorList;
    }
}